package org.jeecgframework.web.material.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.material.entity.FlowDetailPayoutEntity;
import org.jeecgframework.web.material.entity.FlowEntity;
import org.jeecgframework.web.material.service.FlowDetailPayoutServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Description: flowDetailPayout
 * @date 2015-08-17 10:01:47
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/flowDetailPayoutController")
public class FlowDetailPayoutController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FlowDetailPayoutController.class);

	@Autowired
	private FlowDetailPayoutServiceI flowDetailPayoutService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "flowDetailPayout")
	public ModelAndView flowDetailPayout(HttpServletRequest request) {
		String flowid = request.getParameter("flowid");
		request.setAttribute("flowid", flowid);
		return new ModelAndView("material/flowDetailPayout/flowDetailPayoutList");
	}
	
	/**
	 * 查询所有支出
	 * 
	 * @return
	 */
	@RequestMapping(params = "queryFlowDetailPayout")
	public ModelAndView queryFlowDetailPayout(HttpServletRequest request) {
		return new ModelAndView("material/flowDetailPayout/queryFlowDetailPayoutList");
	}
	
	/**
	 * flowDetailPayout新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAddOrUpdate")
	public ModelAndView goAdd(FlowDetailPayoutEntity flowDetailPayout, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(flowDetailPayout.getId())) {
			flowDetailPayout = flowDetailPayoutService.getEntity(FlowDetailPayoutEntity.class, flowDetailPayout.getId());
			req.setAttribute("flowDetailPayoutPage", flowDetailPayout);
		}
		String flowid = req.getParameter("flowid");
		req.setAttribute("flowid", flowid);
		return new ModelAndView("material/flowDetailPayout/flowDetailPayout");
	}
	

	/**
	 * 支出列表
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(FlowDetailPayoutEntity flowDetailPayout,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(FlowDetailPayoutEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, flowDetailPayout, request.getParameterMap());
		try{
		//自定义追加查询条件
			String flowid = request.getParameter("flowid");
			if(StringUtils.isNotEmpty(flowid)){
				cq.createAlias("flowId", "flowId");
				cq.eq("flowId.id", flowid);
			}else{
				TSUser user = ResourceUtil.getSessionUserName();
				cq.createAlias("companyId", "companyId");
				cq.eq("companyId.id", user.getCompany().getId());
			}
			cq.addOrder("payoutTime", SortDirection.desc);
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.flowDetailPayoutService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 添加flowDetailPayout
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAdd(FlowDetailPayoutEntity flowDetailPayout, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if(StringUtil.isEmpty(flowDetailPayout.getId())){
			message = "支出添加成功";
			try{
				String flowid = request.getParameter("flowid");
				FlowEntity flow = new FlowEntity();
				flow.setId(flowid);
				flowDetailPayout.setFlowId(flow);
				TSUser user = ResourceUtil.getSessionUserName();
				flowDetailPayout.setCompanyId(user.getCompany());
				flowDetailPayoutService.save(flowDetailPayout);
				systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
			}catch(Exception e){
				e.printStackTrace();
				message = "支出添加失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}else{
			message = "支出更新成功";
			FlowDetailPayoutEntity t = flowDetailPayoutService.get(FlowDetailPayoutEntity.class, flowDetailPayout.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(flowDetailPayout, t);
				flowDetailPayoutService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "支出更新失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}
		
		return j;
	}
	

	/**
	 * 删除flowDetailPayout
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(FlowDetailPayoutEntity flowDetailPayout, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		flowDetailPayout = systemService.getEntity(FlowDetailPayoutEntity.class, flowDetailPayout.getId());
		message = "支出删除成功";
		try{
			flowDetailPayoutService.delete(flowDetailPayout);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "支出删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除flowDetailPayout
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "支出删除成功";
		try{
			for(String id:ids.split(",")){
				FlowDetailPayoutEntity flowDetailPayout = systemService.getEntity(FlowDetailPayoutEntity.class, 
				id
				);
				flowDetailPayoutService.delete(flowDetailPayout);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "支出删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

}
